<?php
/*
 * file/upload-file.phtml
 */

    use  \RHX\Model\Manager\FileManager;
    $title = $titleView != '' ? $titleView : "Téléchargement de fichier";
    $this->headTitle($title);    
    $repertory = str_replace('public/', '', $repertory);    
?>

<div class="container-fluid">
    <div class="content-head">
        <h4><?php echo $title; ?></h4>
    </div>
    <div class="content-body">
        <div class="preview">
            <?php if (is_null($fichier)): ?>
                <?php if ($error) : ?>       
                    <div class="info"><?php echo $error; ?></div>
                <?php else: ?>            
                <?php endif; ?>
            <?php else: ?>
                <div class="upload-file-info">
                   
                    <?php if (in_array($extension, array('jpg', 'png', 'gif'))) :?> 
                        <img src="<?php echo $this->basePath() . '/'. $repertory . $this->destination ?>" width="200" height="250">
                    <?php else :?> 
                        <span>  <i class="upload-icone-file"></i> <?php echo $fichier; ?></span>
                    <?php endif ;?> 
                </div>
                <div class="row">
                    <span class="upload-file-warning">Vous devez-valider pour prendre en compte le fichier</span>
                    <button id="upload-file-btn-valid" class="btn btn-info" style="float: right;" onclick="validate();">Valider</button>
                </div>
            <?php endif; ?>
        </div>
        <form id="upload-file-form" class="well" action="" method="post" enctype="multipart/form-data">
            <input type="file" id="<?php echo $flag ;?>-file" name="<?php echo $flag ;?>" onchange="submit();" accept="">
            <input type="hidden" name="id_elt" value="<?php echo (int) $idElt; ?>">
            
             <?php if ( $flag == FileManager::SITE_DOC_FILE ): ?>                
                <input type="hidden" name="conserve_name" value="0">
            <?php endif;?>
        </form>
    </div>
</div>

<?php if ( ! in_array($flag, array(FileManager::SITE_DOC_FILE) ) ): ?> 
<script type="text/javascript">
    function validate() {
            <?php if ((int) $idElt == 0):  ?> <?php //page ajout ?>
                         <?php 
                            if($flag != FileManager::EMPLOYE_AVENANT_FILE) :
                                $flag_upl = $flag.'-upload';
                                $flag_lib = $flag.'_libelle';
                            else:
                                $flag_upl = $flag.'-upload-adding';
                                $flag_lib = $flag.'_libelle-adding';
                            endif; 
                        ?>   

                        domFile = window.opener.document.getElementById('<?php echo $flag_lib ;?>');
                        $domFile = $(domFile);
                        $domFile.text('<?php echo $fichier; ?>');
                        //$domFile.text('<?php echo $this->basePath() . '/'.$repertory . $this->destination ?>');

                        dataFile = window.opener.document.getElementById('data-<?php echo $flag ;?>');
                        $dataFile = $(dataFile);
                        $dataFile.val('<?php echo $this->destination; ?>');

                        dataFileExt = window.opener.document.getElementById('data-<?php echo $flag ;?>-ext');
                        $ext = $(dataFileExt);
                        $ext.val('<?php echo $this->extension; ?>');

                        btnTelecharger = window.opener.document.getElementById('<?php echo $flag_upl ;?>');
                        $btnTelecharger = $(btnTelecharger);
                        $btnTelecharger.text('Changer le fichier');

                        <?php if($flag == FileManager::EMPLOYE_RIB_FILE) :?>
                            $btnTelecharger.addClass('btn-upl-right');
                        <?php endif; ?>    

            <?php else: ?> <?php //page edition ?>
                        domFile = window.opener.document.getElementById('<?php echo $flag ;?>_link');
                        $domFile = $(domFile);
                        $domFile.attr('href', '<?php echo $this->basePath() . '/'.$repertory . $this->destination ?>').show();

                        //change bouton valider in page parent
                        btnTelecharger = window.opener.document.getElementById('<?php echo $flag ;?>-upload');
                        $btnTelecharger = $(btnTelecharger);
                        $btnTelecharger.text('Changer');

                        //update data in base            
                        ajax = $.ajax({
                            type: 'post',
                            url: '<?php echo $this->url('RHX/default', array('controller' => $actionSaving['controller'], 'action' => $actionSaving['action'] )) ?>',
                            data: '&name=<?php echo $flag ;?>&pk=<?php echo $this->idElt; ?>&value=' + encodeURI('<?php echo $this->destination; ?>')
                        });

                        <?php if($flag == FileManager::EMPLOYE_AVENANT_FILE ) :?>
                            $('#<?php echo FileManager::EMPLOYE_AVENANT_FILE  ; ?>-info-hasFile').remove();
                        <?php endif; ?> 
            <?php endif; ?>
                    window.close();
                }
            </script>
    
<?php else:  // site ?> 

    <script type="text/javascript">
        function validate() {
            <?php $isPageAjout = false; ?>
                    
            <?php if ($isPageAjout):  //page ajout ?>
                
            <?php else: //page edition ?>  
                
                <?php if ( $flag == FileManager::SITE_DOC_FILE ): //ajout documents au site ?>
                    
                     ajax = $.ajax({
                            type: 'post',
                            url: '<?php echo $this->url('RHX/default', array('controller' => $actionSaving['controller'], 'action' => $actionSaving['action'] )) ?>',
                            data: '&name=<?php echo $flag ;?>&pk=<?php echo $this->idElt; ?>&value=' + encodeURI('<?php echo $this->destination; ?>') 
                        });
                    ajax.done(function(data){
                        if(data.success){
                            $domLinkDoc = $(window.opener.document.getElementById('site-documents'));                    
                            $domCloseDoc = $(window.opener.document.getElementById('doc-close'));     
                            //close popup
                            $domCloseDoc.trigger('click');     
                            //$domLinkDoc.trigger('click');

                            $domLinkDoc.find('#site-doc-count').html(data.nbCountDoc);                            
                        }
                        window.close();
                    });
                     ajax.fail(function(data){
                        
                        window.close();
                    });
                    
                <?php endif; ?> 
                    
            <?php endif; ?>
                //window.close();
        }
    </script>

<?php endif; ?>


<style type="text/css">
    .upload-icone-file{
        display: inline-block;
        width: 32px;
        height: 32px;
        background: url('<?php echo $this->basePath() ?>/img/rhx/file-32x32.png') no-repeat;
    }
    .upload-file-info{
        background: none repeat scroll 0 0 #DEDEDE;
	border: 1px solid #CCCCCC;
	color: #555555;
	display: block;
	margin: 5px auto;
	width: 99%;
    }
    .upload-file-info span{
        line-height: 32px;
    }
    #upload-file-form{
        position: absolute;
        bottom: -20px;
        left: 0px;
        width: 100%;
    }
    #upload-file-form [type=file]{
        color: red;
    }
    .upload-file-warning{
        font-size: 0.9em;
        color: #6666ff;
        font-style: italic;
        text-indent: 5px;
    }
    
    .upload-file-info img {
    border: 4px solid #FAFAFA;
    display: block;
    margin: 0 auto;
}
</style>